package temp.lho.lws.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import temp.lho.lws.service.*;
import temp.lho.lws.utils.GenerateUtil;
import temp.lho.lws.vo.Accident;
import temp.lho.lws.vo.LayuiResult;
/**
 * 没用的方法
 */
import java.util.List;

@RestController
@RequestMapping("/warranty")
public class CRUDContrller {
    @Autowired
    private IWarrantyInfo1Service warrantyInfoService;
    @Autowired
    private IClaimMainInfoService claimMainInfoService;
    @Autowired
    private ICaseReportInfoService caseReportInfoService;
    @Autowired
    private IFlowinfomationService flowinfomationService;
    @Autowired
    private IDispatchedWorkerInfo1Mapper iDispatchedWorkerInfo1Mapper;

    @PostMapping("/getAll")
    public LayuiResult getAll(@RequestBody Accident accident) {
        //System.out.println("进入getAll方法");
        int warranty = warrantyInfoService.selectById(accident.getWarrantyId());
        ///System.out.println(warranty);
        accident.setTaskpId(0);
        accident.setTaskName("报案");
        accident.setFlowId(GenerateUtil.toHex(System.currentTimeMillis()));
        LayuiResult layuiResult =null;
        System.out.println(accident);
        if(warranty>0){
            try {
                caseReportInfoService.insert(accident);
                claimMainInfoService.insert(accident);
                flowinfomationService.insert(accident);
                layuiResult = new LayuiResult(0, "", 2, "新增出险信息成功！");
            }catch (Exception e){
                layuiResult = new LayuiResult(0, "", 1, "新增出险信息失败，输如的信息有误！");
            }

        }else{
            layuiResult = new LayuiResult(0, "", 0, "保单号不存在");
        }
        return layuiResult;
    }

    @PostMapping("/updateAll")
    public LayuiResult updateAll(@RequestBody Accident accident) {
        int warranty = warrantyInfoService.selectById(accident.getWarrantyId());
        LayuiResult layuiResult =null;
        accident.setClaimMainId(claimMainInfoService.selectById(accident));
        //System.out.println("111111"+claimMainInfoService.selectById(accident));
        //System.out.println("222222"+accident);
        if(warranty>0){
            try {
                claimMainInfoService.CMupdate(accident);
                caseReportInfoService.CRupate(accident);
                int jobState=accident.getJobState();
                String claimMainId=accident.getClaimMainId();
                flowinfomationService.Fupdate(jobState,claimMainId);
                layuiResult = new LayuiResult(0, "修改成功", 1, "修改成功！");
            }catch (Exception e){
                layuiResult = new LayuiResult(0, "", 2, "修改失败，输如的信息有误！");
            }
        }else{
            layuiResult = new LayuiResult(0, "", 0, "保单号不存在");
        }
        return layuiResult;
    }

    @PostMapping("/delete")
    public LayuiResult delete(@RequestBody Accident accident) {
        LayuiResult layuiResult =null;
        accident.setClaimMainId(claimMainInfoService.selectById(accident));
        try {
            String claimMainId=accident.getClaimMainId();
            flowinfomationService.Fupdate(6,claimMainId);
            layuiResult = new LayuiResult(0, "", 1, "删除成功！");
        }catch (Exception e){
            layuiResult = new LayuiResult(0, "", 2, "删除失败！");
        }
        return layuiResult;
    }

    @PostMapping("/insert")
    public LayuiResult insert(@RequestBody Accident accident,String userId) {
        //System.out.println(accident);
        accident.setTaskpId(1);
        accident.setTaskName("调度");
        accident.setFlowId(GenerateUtil.toHex(System.currentTimeMillis()));
        accident.setClaimMainId(claimMainInfoService.selectById(accident));
        accident.setAssignedName("刘憨憨");
        accident.setAssignedId("2");
        //System.out.println(accident.getClaimMainId());
        LayuiResult layuiResult =null;
        if(userId==null || userId=="" || userId=="0" || userId.equals(0) ){
            layuiResult = new LayuiResult(0, "", 1, "请指定派工人员！");
        }else {
            try {
                String claimMainId=accident.getClaimMainId();
                iDispatchedWorkerInfo1Mapper.insert(claimMainId,userId);
                flowinfomationService.insert(accident);
                accident.setTaskpId(2);
                accident.setTaskName("查勘定损");
                flowinfomationService.insert(accident);
                layuiResult = new LayuiResult(0, "", 2, "派工成功！");
            }catch (Exception e){
                layuiResult = new LayuiResult(0, "", 0, "派工失败！");
            }
        }
        return layuiResult;
    }
}
